package com.qys.livesMall.anchor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.anchor.mapper.H2AnchorMapper;
import com.qys.livesMall.anchor.model.dto.lives.app.ExchangeGoldDTO;
import com.qys.livesMall.anchor.model.dto.lives.app.GiveGiftDTO;
import com.qys.livesMall.anchor.model.dto.lives.sys.LivesGiftSaveDTO;
import com.qys.livesMall.anchor.model.dto.task.SellTaskBasePageDTO;
import com.qys.livesMall.anchor.model.dto.task.sys.SysLivesGiftPageDTO;
import com.qys.livesMall.anchor.model.entity.AnchorLivesGift;
import com.qys.livesMall.anchor.mapper.AnchorLivesGiftMapper;
import com.qys.livesMall.anchor.model.entity.AnchorLivesGiftRecord;
import com.qys.livesMall.anchor.model.pojo.LivesGiftRecordPOJO;
import com.qys.livesMall.anchor.model.vo.lives.app.AppLivesGiftListVO;
import com.qys.livesMall.anchor.model.vo.lives.sys.SysLivesGiftListVO;
import com.qys.livesMall.anchor.model.vo.task.sys.SysSellLivesAnchorPageVO;
import com.qys.livesMall.anchor.service.AnchorLivesGiftRecordService;
import com.qys.livesMall.anchor.service.AnchorLivesGiftService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.anchor.service.H2AnchorService;
import com.qys.livesMall.app.user.model.dto.sys.SysUserBalanceChangeDTO;
import com.qys.livesMall.app.user.model.enums.UserBalanceLogTypeEnum;
import com.qys.livesMall.app.user.service.AppUserGoldAccountService;
import com.qys.livesMall.app.user.service.IH2AppUserBalanceService;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.utils.MessageUtils;
import com.qys.livesMall.framework.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 直播礼物表 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-06-21
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AnchorLivesGiftServiceImpl extends ServiceImpl<AnchorLivesGiftMapper, AnchorLivesGift> implements AnchorLivesGiftService {



    @Override
    public Page<SysLivesGiftListVO> pageBySys(SysLivesGiftPageDTO queryDTO) {
        Page<SysLivesGiftListVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return baseMapper.pageBySys(page, queryDTO);
    }

    @Override
    public void saveOrUpdate(LivesGiftSaveDTO saveDTO) {
        log.info("管理员增编直播礼物信息,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), saveDTO);
        AnchorLivesGift livesGift = new AnchorLivesGift();
        BeanUtils.copyProperties(saveDTO, livesGift);
        if (null == saveDTO.getId()) {
            //save
            livesGift.setDeleted((byte) 0);
            livesGift.setStatus((byte) 0);
            livesGift.setCreateTime(LocalDateTime.now());
        } else {
            //update
            livesGift.setUpdateTime(LocalDateTime.now());
            livesGift.setUpdateBy(SecurityUtils.getUsername());
        }
        saveOrUpdate(livesGift);
    }

    @Override
    public void deleteById(Long id) {
        log.info("管理员删除直播礼物信息,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), id);
        baseMapper.deleteById(id);
    }

    @Override
    public void shelves(Long id) {
        log.info("管理员执行线上理财上下架操作,操作人：{}，操作信息：{}", SecurityUtils.getUsername(), id);
        AnchorLivesGift livesGift = baseMapper.selectById(id);
        if (null == livesGift) {
            throw new QysException("请检查ID");
        }
        livesGift.setStatus((byte) (livesGift.getStatus() == 0 ? 1 : 0));
        baseMapper.updateById(livesGift);
    }

    @Override
    public Page<AppLivesGiftListVO> pageByApp(SellTaskBasePageDTO queryDTO) {
        Page<AppLivesGiftListVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return baseMapper.pageByApp(page, queryDTO);
    }


    @Override
    public SysLivesGiftListVO queryById(Long id) {
        return BeanUtil.copyProperties(getById(id), SysLivesGiftListVO.class);
    }
}
